package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.SubmitEvaluationBean;
import com.sikao.app.dao.SubmitEvaluationModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;


public class EvaluateActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private TextView tvType1;
	private EditText editEvaluate;
	private TextView tvType2;
	private RatingBar ratingBar1;
	private RatingBar ratingBar2;
	private RatingBar ratingBar3;
	private Button btn;

	private SubmitEvaluationBean submitEvaluationBean;
	private SubmitEvaluationModel submitEvaluationModel;
	private String beanId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_evaluate);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		beanId = getIntent().getStringExtra("bean");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		tvType1 = (TextView) findViewById(R.id.tv_type1);
		editEvaluate = (EditText) findViewById(R.id.edit_evaluate);
		tvType2 = (TextView) findViewById(R.id.tv_type2);
		ratingBar1 = (RatingBar) findViewById(R.id.ratingBar1);
		ratingBar2 = (RatingBar) findViewById(R.id.ratingBar2);
		ratingBar3 = (RatingBar) findViewById(R.id.ratingBar3);
		btn = (Button) findViewById(R.id.btn);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		btn.setOnClickListener(this);
	}


	@Override
	protected void init() {
		titleTv.setText("发表评价");
		backIv.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.btn:
				submitEvaluation();
				break;
			default:
				break;
		}
	}

	private void submitEvaluation() {
		if (submitEvaluationBean == null){
			submitEvaluationBean = new SubmitEvaluationBean();
		}
		submitEvaluationBean.setOrderId(beanId);
		submitEvaluationBean.setAction("comment");
		submitEvaluationBean.setContent(editEvaluate.getText().toString());
		submitEvaluationBean.setScore1((int)ratingBar1.getRating()+"");
		submitEvaluationBean.setScore2((int)ratingBar2.getRating()+"");
		submitEvaluationBean.setScore3((int)ratingBar3.getRating()+"");
		if (submitEvaluationModel == null) {
			submitEvaluationModel = new SubmitEvaluationModel(this, new OnNetBackListener<Bean>() {
				@Override
				public void netBack(Bean data) {
					if (data == null){
						return;
					}
					if (data.getStatus().equals("1")){
						Toast.makeText(EvaluateActivity.this, "评价成功", Toast.LENGTH_SHORT).show();
						Intent intent = getIntent();
						setResult(RESULT_OK,intent);
						finish();
					}else{
						Toast.makeText(EvaluateActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}

				}
			});
		}
		submitEvaluationModel.request(JSONUtil.toJSON(submitEvaluationBean), true);
	}
}
